<?php if(!defined('IN_FIRADIO')) {header('HTTP/1.1 403 Forbidden');exit('<h1>Access Denied</h1>');}?>
<?php $GLOBALS['script'][] = function() {?><script>
vue.data.apidata = {};
vue.data.choice_host = {};
vue.data.choice_pack = {};
vue.data.choice_ntuser = {};
vue.data.choice_ntbalance = {};
vue.data.disabled = 0;
vue.data.flag = 'list';
vue.data.form = {};
vue.data.form.host_id = null;
vue.data.form.pack_id = null;
vue.data.form.user_id = null;
vue.data.form.balance_id = null;
vue.data.display_field = {};
vue.data.display_field.host_type = true;
vue.data.display_field.host_area = false;
vue.data.display_field.host_name = false;
vue.data.display_field.host_title = true;
vue.data.display_field.host_osname = true;
vue.data.display_field.isp_title = false;
var submit_after_result = function (that) {
};
function intval(i) {
    var i = parseInt(i, 10);
    if (isNaN(i)) i = 0;
    return i;
}
vue.methods.point_to_yuan = function (cpuseconds) {
    return (Math.floor(cpuseconds) / 10000) + '元';
};
vue.methods.toTop = function() {
    top.document.documentElement.scrollTop = 0;
    top.document.getElementById('iframe_content').height = 0;

};
vue.methods.getRowByRowsId = window.getRowByRowsId;
vue.methods.submit_list = function () {
    const form = {};
    this.ApiPost('/panel/yun/nthost/list.php', form).then(function (data) {
        vue.data.apidata = data.data;
        vue.data.form.user_id = vue.data.apidata.rows_ntuser[0]['id'];
        vue.methods.select_user_id_change();
        submit_after_result();
    }, function (data) {
        console.log('reject:', data);
        submit_after_result();
    }).catch(function (reason) {
        console.log('catch:', reason);
        submit_after_result();
    })
};
function checkres(res) {
    if (res.data.hasOwnProperty('go')) {
        top.location.hash = res.data.go + '.html';
    }
}
vue.methods.submit = function () {
    const that = this;
    var msg = "重要提醒：";
    msg += this.choice_host['remind'] + "\r\n";
    msg += "确定要选购这台主机吗？";
    if (confirm(msg)) {
        vue.methods.submit_ok(that);
    }
};
vue.methods.submit_ok = function (that) {
    //const that = this;
    const form = window.clone(that.form);
    that.disabled++;
    var urlPath = '/panel/yun/nthost/useradd.php';
    that.ApiPost(urlPath, form).then(function (res) {
        checkres(res);
    }, function (res) {
        console.log('reject:', res);
    }).then(function (res) {
        that.disabled--;
    }).catch(function (reason) {
        that.disabled--;
        console.log('catch:', reason);
    })
};
vue.methods.dayToSecond = function (day) {
    return Math.round(day / 86.4) / 1000;
};
vue.methods.dayToMinute = function (day) {
    return Math.round(day / 144) / 10;
};
vue.methods.select_user_id_change = function () {
    vue.data.choice_ntuser = vue.methods.getRowByRowsId(vue.data.apidata.rows_ntuser, vue.data.form.user_id);
    vue.data.form.qquin = vue.data.choice_ntuser.qquin;
};
vue.mounted = function () {
    vue.methods.submit_list();
};
vue.computed = {};
vue.computed.rows_ntbalance2 = function () {
    if (!vue.hasOwnProperty('data')) return [];
    if (!vue.data.hasOwnProperty('apidata')) return [];
    if (!vue.data.apidata.hasOwnProperty('rows_ntbalance')) return [];
    const rows = [];
    const len = vue.data.apidata.rows_ntbalance.length;
    for (var i = 0; i < len; i++) {
        const row = vue.data.apidata.rows_ntbalance[i];
        if (row.user_id != vue.data.form.user_id) {
            continue;
        }
        if (row.balance <= 0) {
            continue;
        }
        rows.push(row);
    }
    if (rows.length > 0) {
        vue.data.form.balance_id = rows[0].id;
        vue.data.choice_ntbalance = rows[0];
    }
    return rows;
};
vue.computed.rows_pack2 = function () {
    if (!vue.hasOwnProperty('data')) return [];
    if (!vue.data.hasOwnProperty('apidata')) return [];
    if (!vue.data.apidata.hasOwnProperty('rows_pack')) return [];
    const allow_packs = vue.data.choice_host.packlist.split(',');
    const rows = [];
    const len = vue.data.apidata.rows_pack.length;
    for (var i = 0; i < len; i++) {
        const row = vue.data.apidata.rows_pack[i];
        if (allow_packs.indexOf(row.id) === -1) continue;
        rows.push(row);
    }
    if (rows.length > 0) {
        vue.data.form.pack_id = rows[0].id;
        vue.data.choice_pack = rows[0];
    }
    return rows;
};
</script><?php }?>

<div class="row">
    <div class="col-xs-12 col-sm-8 col-md-6 col-lg-6" v-if="flag=='list'">
        <div class="box box-info" v-if="apidata.rows_host">
            <div style="padding-left: 20px;">
                显示列：
                <label><input v-model="display_field.host_name" type="checkbox" />机号</label>
                <label><input v-model="display_field.host_type" type="checkbox" />类型</label>
                <label><input v-model="display_field.host_area" type="checkbox" />地域</label>
                <label><input v-model="display_field.host_title" type="checkbox" />主机特点</label>
                <label><input v-model="display_field.host_osname" type="checkbox" />操作系统</label>
                <label><input v-model="display_field.isp_title" type="checkbox" />线路</label>
            </div>
            <table id="example1" class="table table-bordered table-striped dataTable" role="grid" aria-describedby="example1_info">
                <thead>
                    <tr role="row">
                        <th v-if="display_field.host_name">机号</th>
                        <th v-if="display_field.host_type">类型</th>
                        <th v-if="display_field.host_area">地域</th>
                        <th v-if="display_field.host_title">主机特点</th>
                        <th v-if="display_field.host_osname">操作系统</th>
                        <th v-if="display_field.isp_title">线路</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr role="row" class="odd" v-for="(row) in apidata.rows_host" align="center">
                        <td v-if="display_field.host_name">{{row['name']}}</td>
                        <td v-if="display_field.host_type">
                            <span v-if="row['type']==1" style="color:green">共享</span>
                            <span v-else-if="row['type']==2" style="color:blue">独立</span>
                            <span v-else>-</span>
                        </td>
                        <td v-if="display_field.host_area">{{row['area']}}</td>
                        <td v-if="display_field.host_title">{{row['title']}}</td>
                        <td v-if="display_field.host_osname">{{row['osname']}}</td>
                        <td v-if="display_field.isp_title">{{row['isp_title']}}{{row['bandwidth_upload_mbps']}}Mbps</td>
                        <td><button class="btn btn-primary btn-xs" type="button" v-on:click="flag='choice';form.host_id=row['id'];choice_host=row">选购</button></td>
                    </tr>
                </tbody>
                <tfoot v-if="0">
                    <tr>
                        <th rowspan="1" colspan="1">-</th>
                    </tr>
                </tfoot>
            </table>
        </div>
        <div v-else>
            <h2>数据载入中，请稍候...</h2>
        </div>
        <div>
            1：<span style="color:green">共享型</span>云主机
            请尽量选择<span style="color: red">Win2012R2</span>操作系统（体验更流畅），<br />
            目前只有【基岩版】MC开服才要用Win2019，<br />
            由于Win2019自带杀毒（<span style="color:green">共享型</span>无权关闭杀毒），
            因此使用某些软件会误报病毒
            (如：QQ机器人、易语言、外挂等)，
            且Win2019登录时会黑屏1-2分钟，建议选用Win2012R2<br />
            <br />
            2：<span style="color:green">共享型</span>云主机没有管理员权限，<br />
            开通后您可存放文件的路径为<span style="color: red">D:\Users\远程用户名</span>，
            桌面、我的文档、下载的<span style="color: red">默认存放路径</span>也都在这里，<br />
            当您销毁主机时<span style="color: red">D:\Users\远程用户名</span>整个文件夹将被删除且无法恢复。<br />
            <span style="color:blue">独立型</span>VPS虽有管理员权限，但容易感染病毒（新手学习用可以）。<br />
            <br />
            3：<span style="color:green">共享型</span>云主机
            将按照CPU实际的使用率收费（不用时还会收取保底）<br />
            <span style="color:blue">独立型</span>VPS
            只按运行时间收费，不会产生额外的点数消耗。
            <br />
        </div>
    </div>
    <div class="col-xs-12 col-sm-8 col-md-6 col-lg-6" v-if="flag=='choice'">
        <form method="post" v-on:submit="submit" onsubmit="return false;">
        <div class="box box-info">
            <div class="box-header with-border">
                <h3 class="box-title">选购云主机</h3>
                <div class="pull-right">
                    <button class="btn btn-primary btn-xs" type="button" v-on:click="flag='list';toTop()">返回列表</button>
                </div>
            </div>
            <div class="box-body form-horizontal">
                <div class="form-group">
                    <label class="col-sm-3 control-label">主机标题</label>
                    <div class="col-sm-9">
                        <div class="form-control">{{choice_host['title']}} ({{choice_host['name']}})</div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">操作系统</label>
                    <div class="col-sm-9">
                        <div class="form-control">{{choice_host['osname']}}</div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">适用范围</label>
                    <div class="col-sm-9">
                        <div class="form-control">{{choice_host['appscope']}}</div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">重要提醒</label>
                    <div class="col-sm-9">
                        <div class="form-control">{{choice_host['remind']}}</div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">联网线路</label>
                    <div class="col-sm-9">
                        <div class="form-control">
                        {{choice_host['isp_title']}}，
                        上行{{choice_host['bandwidth_upload_mbps']}}Mbps，
                        下行{{choice_host['bandwidth_download_mbps']}}Mbps
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="select_pack_id" class="col-sm-3 control-label">
                        选择套餐
                    </label>
                    <div class="col-sm-9">
                        <select v-if="choice_host['type']==1" class="form-control" id="select_pack_id" v-model="form.pack_id" v-on:change="choice_pack=getRowByRowsId(apidata.rows_pack, form.pack_id)">
                            <option v-for="(row) in rows_pack2"
                            :value="row.id"
                            >
                            [{{row['name']}}]{{row['title']}}, {{point_to_yuan(row['dayminpoint']*choice_pack['holddays'])}}押金永久使用
                            </option>
                        </select>
                        <select v-if="choice_host['type']==2" class="form-control" id="select_pack_id" v-model="form.pack_id" v-on:change="choice_pack=getRowByRowsId(apidata.rows_pack, form.pack_id)">
                            <option v-for="(row) in rows_pack2"
                            :value="row.id"
                            >
                            [{{row['name']}}]{{row['title']}}, 每天 {{point_to_yuan(row['dayminpoint'])}}, 每月 {{point_to_yuan(row['dayminpoint']*30)}}
                            </option>
                        </select>
                        <div class="">
                            您选择的是[{{choice_pack['name']}}]{{choice_pack['title']}} 说明：{{choice_pack['note']}}<br />
                            <span v-if="choice_host['type']==1">
                                永久存储空间：<span style="color:red">{{choice_pack['limit_storagegb']}} GB</span>
                                （另赠 <span style="color:red">{{choice_pack['limit_storagegb']}} GB</span> 临时空间）
                                <br />
                            </span>
                            <span v-if="choice_host['type']==2">
                                CPU限制：<span style="color:red">{{choice_pack['limit_cpucore']}}</span>核，
                                保证运存：<span style="color:red">{{choice_pack['limit_ramgb']}}GB</span>
                                系统盘空间：<span style="color:red">{{choice_pack['limit_storagegb']}}GB</span>
                                （无独立数据盘）<br />
                            </span>
                            <br />
                            <span v-if="choice_host['type']==2">
                                独立VPS每日<font color="red">固定</font>收费
                                <span style="color:red">{{point_to_yuan(choice_pack['dayminpoint'])}}</span>,
                                每月<span style="color:red">{{point_to_yuan(choice_pack['dayminpoint']*30)}}</span>
                            </span>
                            <span v-else>
                                扣除{{point_to_yuan(choice_pack['dayminpoint']*choice_pack['holddays'])}}押金即可永久使用（销毁主机后押金可退回）
                            </span>
                            <br />
                            <span v-if="choice_host['type']==2">
                                独立VPS<span style="color:red">按分钟计费</span>，
                                每分钟计费<span style="color:red">{{point_to_yuan(dayToMinute(choice_pack['dayminpoint']))}}</span>，
                                如您开机只玩60分钟立马销毁只收<span style="color:red">{{point_to_yuan(dayToMinute(choice_pack['dayminpoint'])*60)}}</span>
                                <br />
                                开通时扣除 {{choice_pack['holddays']}} 天押金合计
                                <span style="color:red">{{point_to_yuan(choice_pack['dayminpoint']*choice_pack['holddays'])}}</span>，<br />
                                押金可在销毁主机后退回（扣除欠款）。<br />
                                为避免产生欠费账单，请确保账户内有充足的余额。<br />
                                欠费账单累计金额超过<span style="color:red">押金</span>将会自动停机！<br />
                            </span>
                            <span v-if="choice_host['type']==1" style="color:blue">
                                <font color="red">注意：共享主机会单独产生CPU计费账单，并非只有押金！</font><br />
                            </span>

                        </div>
                    </div>
                </div>
                <div class="form-group" v-if="apidata.rows_ntuser&&apidata.rows_ntuser.length>0">
                    <label for="select_user_id" class="col-sm-3 control-label">远程桌面账号</label>
                    <div class="col-sm-9">
                        <select class="form-control" id="select_user_id" v-model="form.user_id" v-on:change="select_user_id_change">
                            <option v-for="(row) in apidata.rows_ntuser"
                            :value="row.id"
                            >
                            {{row['username']}} 可用余额:{{point_to_yuan(row['balance'])}}
                            </option>
                        </select>
                        <div v-if="rows_ntbalance2.length==0&&form.user_id">
                            <span style="color:red">您选择的远程桌面账户里没有任何余额账户，请到【飞儿云平台】微信公众号充值</span>
                        </div>
                        <span v-if="choice_host['type']==2">
                            独立机的管理账号可能为
                            <font color="red">root</font>
                            或
                            <font color="red">Administrator</font>
                            开通后请注意查看
                        </span>
                    </div>
                </div>
                <div v-else>
                    <span style="color:red">请先添加【远程桌面账号】</span>
                </div>
                <div class="form-group" v-if="rows_ntbalance2.length>0">
                    <label for="select_balance_id" class="col-sm-3 control-label">押金缴纳账户</label>
                    <div class="col-sm-9">
                        <select class="form-control" id="select_balance_id" v-model="form.balance_id" v-on:change="choice_ntbalance=getRowByRowsId(apidata.rows_ntbalance, form.balance_id)">
                            <option v-for="(row) in rows_ntbalance2"
                            :value="row.id"
                            >
                            {{row['acctype']}} 余额:{{point_to_yuan(row['balance'])}}
                            </option>
                        </select>
                        <div>
                            <span style="color:blue">扣除{{point_to_yuan(choice_pack['dayminpoint']*choice_pack['holddays'])}}后，剩余{{point_to_yuan(choice_ntbalance['balance']-choice_pack['dayminpoint']*choice_pack['holddays'])}}</span>
                            <span style="color:red" v-if="choice_ntbalance['balance']<choice_pack['dayminpoint']*choice_pack['holddays']">余额不足</span>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="qquin" class="col-sm-3 control-label">预留QQ号码</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control"
                        id="qquin"
                        v-model="form.qquin"
                        :disabled="disabled>0"
                        required
                        placeholder="请设置预留QQ号码或手机号码" />
                    </div>
                </div>
            </div>
            <div class="box-footer">
                <div class="col-xs-6">
                    <button type="button" class="btn btn-default btn-block btn-flat" v-on:click="flag='list';toTop()">返回列表</button>
                </div>
                <div class="col-xs-6">
                    <button type="submit" class="btn btn-info btn-block btn-flat" :disabled="disabled>0">申请主机</button>
                </div>
            </div>
        </div>
        </form>
    </div>
</div>
